package com.jic.member.impl.power;

import java.lang.Long;
import java.util.Date;
import java.util.List;


import com.jic.common.base.vo.AdminOperate;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.entity.MememberGrade;
import com.jic.member.entity.power.MegradeType;
import com.jic.member.mapper.MememberGradeMapper;
import com.jic.member.mapper.power.MegradeTypeMapper;
import com.jic.member.service.power.MegradeTypeService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 等级类型表
 * @author : zxl
 * @email : 
 * @since : 2020-07-01 03:15:14
 * @version : v1.0.0
 */
@Service
public class MegradeTypeServiceImpl implements MegradeTypeService {

    @Resource
    private MegradeTypeMapper megradeTypeMapper;
    @Resource
    private MememberGradeMapper mememberGradeMapper;

    @Override
    public RestResult insert(MegradeType megradeType) {
        RestResult result = new RestResult();
        AdminOperate adminOperate = megradeType.getAdminOperate();
        megradeType.setDeptId(adminOperate.getDeptId());
        megradeType.setDeptType(adminOperate.getDeptType().getCode().longValue());
        megradeType.setDeleteFlag(0);
        //同一组织下 类型名称不可重复
        List<MegradeType> query = megradeTypeMapper.query(megradeType);
        if(CollectionUtils.isNotEmpty(query)){
            result.setCode("9999");
            result.setMsg("该等级类型名称已存在,不能重复");
            return result;
        }
        if (null ==megradeType.getId()){
            megradeType.setCreator(adminOperate.getOperator());
            megradeType.setDeleteFlag(0);
              megradeTypeMapper.insert(megradeType);
        }else {
            megradeType.setUpdateTime(new Date());
             megradeTypeMapper.update(megradeType);
        }
        result.setCode("0000");
        return result;
    }

    @Override
    public RestResult delete(Long id) {
        RestResult result = new RestResult();
        //查询等级类型是否关联等级
        MememberGrade memberGrade = new MememberGrade();
        memberGrade.setDeleteFlag(0);
        List<MememberGrade> resultMememberGrade = mememberGradeMapper.query(memberGrade);
        if(CollectionUtils.isNotEmpty(resultMememberGrade)){
            result.setCode("9999");
            result.setMsg("该类型下已关联店铺等级，请解除关联后再进行操作");
            return result;
        }
        megradeTypeMapper.delete(id);
        result.setCode("0000");
        return result;
    }

    @Override
    public long update(MegradeType megradeType) {
        return megradeTypeMapper.update(megradeType);
    }

    @Override
    public MegradeType queryByPrimaryKey(Long id){
        return megradeTypeMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<MegradeType> query(MegradeType megradeType){
        AdminOperate adminOperate = megradeType.getAdminOperate();
        megradeType.setDeleteFlag(0);
        if (adminOperate.getDeptType().getCode()==1){
            megradeType.setDeptId(adminOperate.getDeptId());
        }
        return megradeTypeMapper.query(megradeType);
    }

    @Override
    public PageResult<MegradeType> queryPage(MegradeType megradeType, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        AdminOperate adminOperate = megradeType.getAdminOperate();
        if (adminOperate.getDeptType().getCode()==1){
            megradeType.setDeptId(adminOperate.getDeptId());
        }
        megradeType.setDeleteFlag(0);
        List<MegradeType> list = megradeTypeMapper.queryPage(startIndex, pageQuery.getLimit(), megradeType);
        long count = megradeTypeMapper.queryPageCount(megradeType);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }
}